package cn.edu.nwsuaf.examcenter.service.serviceImpl;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Service;
import org.springframework.ui.ModelMap;

import cn.edu.nwsuaf.examcenter.model.PageModel;
import cn.edu.nwsuaf.examcenter.pojo.Cource;
import cn.edu.nwsuaf.examcenter.service.CourceService;

@Service(value="courceService")
public class CourceServiceImpl extends BaseServiceImpl<Cource> implements CourceService {

	@Override
	public List<Cource> jsonLoadCource(String key) {
		// TODO Auto-generated method stub
		String hql = null;
		List<Cource> list = null;
		if(key!=null){
			hql = "FROM Cource where courceName like(%?%)";
			String[] param = {key};
			list = super.findAll(hql, param);
		}else{
			list = super.findAll(Cource.class);
		}
		return list;
	}

	@Override
	public String addCource(Cource cource) {
		// TODO Auto-generated method stub
		super.saveData(cource);
		return "success";
	}

	@Override
	public String deleteCource(String courceNo) {
		// TODO Auto-generated method stub
		Cource cource = super.getById(Cource.class, courceNo);
		super.deleteData(cource);
		return "success";
	}

	@Override
	public String deleteCourceList(String array) {
		// TODO Auto-generated method stub
		String[] courceNos = array.split(",");
		List<Cource> list = new ArrayList<>();
		for(String courceNo : courceNos){
			list.add(super.loadById(Cource.class, courceNo));
		}
		super.deleteDataList(list);
		return "sccess";
	}

	@Override
	public void exportCource(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub

	}

	@Override
	public void loadCource(ModelMap modelMap) {
		// TODO Auto-generated method stub
		PageModel<Cource> pageModel = new PageModel<>();
		List<Cource> list = super.findAll(Cource.class);
		pageModel.setTotalrows(list.size());
		pageModel.setList(list);
		modelMap.addAttribute("pageModel",pageModel);
	}

	@Override
	public List<Cource> jsonSearchCource(String courceName) {
		// TODO Auto-generated method stub
		List<Cource> list = new ArrayList<>();
		if(courceName==null){
			list = super.findAll(Cource.class);
		}else{
			String[] param = {"%"+courceName+"%"};
			String hql = "FROM Cource as cource where cource.courceName=?";
			list = super.findAll(hql, param);
		}
		return list;
	}

}
